using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;
using System.IO;
namespace DAL
{
    public class tbHinhAnhDAL
    {
        private SqlConnection cn = null;
        public tbHinhAnhDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public void Insert(tbHinhAnh hinhanh)
        {
            SqlCommand cm = new SqlCommand("sp_HinhAnh_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenHinh", hinhanh.TenHinh);
            cm.Parameters.AddWithValue("@DuongDan", hinhanh.DuongDan);
            cm.Parameters.AddWithValue("@IdSanPham",hinhanh.IdSanPham );
            cm.ExecuteNonQuery();
        }

        public void Update(tbHinhAnh hinhanh)
        {
            SqlCommand cm = new SqlCommand("sp_HinhAnh_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdHinhAnh", hinhanh.IdHinhAnh);
            cm.Parameters.AddWithValue("@TenHinh", hinhanh.TenHinh);
            cm.Parameters.AddWithValue("@DuongDan", hinhanh.DuongDan);
            cm.Parameters.AddWithValue("@IdSanPham", hinhanh.IdSanPham);
            cm.ExecuteNonQuery();
        }

        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("sp_HinhAnh_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdHinhAnh", Id);
            cm.ExecuteNonQuery();
        }
        public void DeleteIdSP(int IdSanPham)
        {   
            string sql = "delete from HinhAnh where IdSanPham='" + IdSanPham + "'";
            SqlCommand cm = new SqlCommand(sql, cn);
            cm.ExecuteNonQuery();
        }
        public string[] DeleteImageByIdSP(int IdSanPham)
        {
            string sql = "select * from HinhAnh where IdSanPham='" + IdSanPham + "'";
            SqlCommand cm = new SqlCommand(sql, cn);
            SqlDataReader dr = cm.ExecuteReader();
            string[] array =new string [100];
            int i=0;
            while (dr.Read())
            {  
                array[i]=  dr.GetValue(1).ToString();
                i++;
            }
            dr.Close();
            return array;
        }
        public tbHinhAnhCollection Get(string Where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_HinhAnh_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbHinhAnhCollection hinhanhcollection = new tbHinhAnhCollection();
            while (dr.Read())
            {
                tbHinhAnh hinhanh = new tbHinhAnh();
                hinhanh.IdHinhAnh  = int.Parse(dr.GetValue(0).ToString());
                hinhanh.TenHinh = dr.GetValue(1).ToString();
                hinhanh.DuongDan = dr.GetValue(2).ToString();
                hinhanh.IdSanPham = int.Parse(dr.GetValue(3).ToString());
                hinhanhcollection.Add(hinhanh);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return hinhanhcollection;
        }
        public int FirstImage(int IdSanPham,int Id)
        {

            SqlCommand cm = new SqlCommand("sp_HinhAnh_GetFirstImage", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdSanPham", IdSanPham);
            cm.Parameters.Add(new SqlParameter("@IdHA", DbType.Int32)).Direction = ParameterDirection.Output;
            cm.ExecuteReader();
            Id = int.Parse(cm.Parameters["@IdHA"].Value.ToString());
            return Id ;

        }
    }
}
